﻿package mortal.component.window
{
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.manager.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.display.*;
    import mortal.common.sound.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class Window extends GSprite implements IUIView, IDragDrop, ILayer, IStageResize, IWindow3D
    {
        protected var _closeBtn:GLoadedButton;
        protected var _isAddClolseButton:Boolean = true;
        protected var _isCenter:Boolean = false;
        protected var _isFirstCenter:Boolean = true;
        protected var _titleSpriteHight:Number = 30;
        protected var _tweenable:Boolean = true;
        protected var contentSprite:Sprite;
        protected var _titleSprite:Sprite;
        protected var _popupSprite:Sprite;
        protected var _content3DSprite:Sprite;
        protected var _contentTopOf3DSprite:Sprite;
        protected var _contentHighest:Sprite;
        protected var _contentX:Number = 0;
        protected var _contentY:Number = 0;
        protected var _titleHeight:Number = 35;
        public var isForbidenDrag:Boolean = false;
        private var _stagerect:Rectangle;
        private var _startX:Number;
        private var _startY:Number;
        private var _baseX:Number;
        private var _baseY:Number;
        protected var _isPositionChange:Boolean = false;
        private var _bgBitmap:ScaleBitmap;
        protected var _layer:ILayer;
        private var _isHide:Boolean = true;
        private var _tweenHide:TweenMax;
        private var _x:int = -1;
        private var _y:int = -1;

        public function Window(param1:ILayer = null)
        {
            this._stagerect = new Rectangle();
            Log.debugMsg(LogType.other, this);
            if (param1 == null)
            {
                this.layer = LayerManager.windowLayer;
            }
            else
            {
                this.layer = param1;
            }
            isDisposeRemoveSelf = false;
            isHideDispose = true;
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onWindowDownHandler);
            return;
        }// end function

        public function get contentTopOf3DSprite() : Sprite
        {
            return this._contentTopOf3DSprite;
        }// end function

        override protected function configUI() : void
        {
            this.configParams();
            this.contentSprite = UIFactory.sprite();
            this.contentSprite.mouseEnabled = false;
            this.contentSprite.x = this._contentX;
            this.contentSprite.y = this._contentY;
            this.$addChild(this.contentSprite);
            this._content3DSprite = UIFactory.sprite();
            this._content3DSprite.mouseEnabled = false;
            this._content3DSprite.x = this._contentX;
            this._content3DSprite.y = this._contentY;
            this.$addChild(this._content3DSprite);
            this._contentTopOf3DSprite = UIFactory.sprite();
            this._contentTopOf3DSprite.mouseEnabled = false;
            this.$addChild(this._contentTopOf3DSprite);
            this._contentTopOf3DSprite.addEventListener(MouseEvent.MOUSE_DOWN, this.onWindowDownHandler);
            this._titleSprite = new Sprite();
            this._titleSprite.name = "titleSprite";
            this._titleSprite.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
            this._contentTopOf3DSprite.addChild(this._titleSprite);
            this._contentHighest = UIFactory.sprite();
            this._contentHighest.mouseEnabled = false;
            this.$addChild(this._contentHighest);
            this._popupSprite = UIFactory.sprite();
            this._popupSprite.mouseEnabled = false;
            this.$addChild(this._popupSprite);
            super.configUI();
            return;
        }// end function

        protected function configParams() : void
        {
            this._contentX = 0;
            this._contentY = 0;
            return;
        }// end function

        override protected function onAddToStageCreate(event:Event) : void
        {
            super.onAddToStageCreate(event);
            this.updateSize();
            return;
        }// end function

        public function get closeBtn() : GLoadedButton
        {
            return this._closeBtn;
        }// end function

        protected function onWindowDownHandler(event:MouseEvent) : void
        {
            if (this.layer is WindowLayer)
            {
                PopupManager.setTop(this);
            }
            else
            {
                this.layer.setTop(this);
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            if (this._isAddClolseButton)
            {
                this.addCloseButton();
            }
            return;
        }// end function

        protected function addCloseButton() : void
        {
            this._closeBtn = UIFactory.gLoadedButton(ImagesConst.Close_upSkin, 0, 0, 22, 20, null);
            this._closeBtn.focusEnabled = true;
            this._closeBtn.name = "Window_Btn_Close";
            this._closeBtn.configEventListener(MouseEvent.CLICK, this.closeBtnClickHandler);
            this._contentTopOf3DSprite.addChild(this._closeBtn);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (this._closeBtn)
            {
                this._closeBtn.name = "";
                this._closeBtn.dispose(param1);
                this._closeBtn = null;
            }
            this._titleHeight = 35;
            this._titleSpriteHight = 30;
            this.isForbidenDrag = false;
            return;
        }// end function

        public function get highestContentContainer() : DisplayObjectContainer
        {
            return this._contentHighest;
        }// end function

        public function get contentContainer() : Sprite
        {
            return this.contentSprite;
        }// end function

        override public function addChild(param1:DisplayObject) : DisplayObject
        {
            return this.contentSprite.addChild(param1);
        }// end function

        override public function removeChild(param1:DisplayObject) : DisplayObject
        {
            if (this.contentSprite.contains(param1))
            {
                return this.contentSprite.removeChild(param1);
            }
            return this.$removeChild(param1);
        }// end function

        override public function addChildAt(param1:DisplayObject, param2:int) : DisplayObject
        {
            return this.contentSprite.addChildAt(param1, param2);
        }// end function

        override public function removeChildAt(param1:int) : DisplayObject
        {
            return this.contentSprite.removeChildAt(param1);
        }// end function

        override public function getChildAt(param1:int) : DisplayObject
        {
            return this.contentSprite.getChildAt(param1);
        }// end function

        override public function get numChildren() : int
        {
            return this.contentSprite.numChildren;
        }// end function

        override public function contains(param1:DisplayObject) : Boolean
        {
            return this.contentSprite.contains(param1);
        }// end function

        protected function $addChild(param1:DisplayObject) : DisplayObject
        {
            return super.addChild(param1);
        }// end function

        protected function $removeChild(param1:DisplayObject) : DisplayObject
        {
            return super.removeChild(param1);
        }// end function

        protected function $addChildAt(param1:DisplayObject, param2:int) : DisplayObject
        {
            return super.addChildAt(param1, param2);
        }// end function

        protected function $removeChildAt(param1:int) : DisplayObject
        {
            return super.removeChildAt(param1);
        }// end function

        protected function onMouseDownHandler(event:MouseEvent) : void
        {
            if (this.isForbidenDrag)
            {
                return;
            }
            this._startX = event.stageX;
            this._startY = event.stageY;
            this._baseX = this.x;
            this._baseY = this.y;
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            Global.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler);
            return;
        }// end function

        override public function set x(param1:Number) : void
        {
            super.x = param1;
            this.dispatchMoveChange();
            return;
        }// end function

        override public function set y(param1:Number) : void
        {
            super.y = param1;
            this.dispatchMoveChange();
            return;
        }// end function

        private function dispatchMoveChange() : void
        {
            this.dispatchEvent(new WindowEvent(WindowEvent.POSITIONCHANGE));
            return;
        }// end function

        private function onMouseUpHandler(event:MouseEvent) : void
        {
            this.removeDrag();
            this.dispatchEvent(new Event(EventName.WindowDragEnd));
            return;
        }// end function

        private function removeDrag() : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler);
            return;
        }// end function

        protected function onMouseMoveHandler(event:MouseEvent) : void
        {
            if (this.isDisposed || event == null)
            {
                return;
            }
            var _loc_2:* = event.stageX;
            var _loc_3:* = event.stageY;
            var _loc_4:* = _loc_2 - this._startX + this._baseX;
            var _loc_5:* = _loc_3 - this._startY + this._baseY;
            _loc_4 = _loc_4 < -this.width + 100 ? (-this.width + 100) : (_loc_4);
            _loc_4 = _loc_4 > this.stage.stageWidth - 100 ? (this.stage.stageWidth - 100) : (_loc_4);
            _loc_5 = _loc_5 < 0 ? (0) : (_loc_5);
            _loc_5 = _loc_5 > this.stage.stageHeight - this._titleHeight ? (this.stage.stageHeight - this._titleHeight) : (_loc_5);
            this.x = _loc_4;
            this.y = _loc_5;
            return;
        }// end function

        protected function setTitleSprite(param1:Number, param2:Number) : void
        {
            if (this._bgBitmap == null)
            {
                this._bgBitmap = ResourceConst.getScaleBitmap(BitmapDataConst.AlphaBMD);
                this._titleSprite.addChildAt(this._bgBitmap, 0);
            }
            this._bgBitmap.width = param1;
            this._bgBitmap.height = param2;
            return;
        }// end function

        protected function closeBtnClickHandler(event:MouseEvent) : void
        {
            SoundManager.instance.soundPlay(SoundTypeConst.UIclose);
            this.hide();
            return;
        }// end function

        public function set titleHeight(param1:Number) : void
        {
            this._titleHeight = param1;
            return;
        }// end function

        public function get titleHeight() : Number
        {
            return this._titleHeight;
        }// end function

        protected function updateSize() : void
        {
            this.updateBtnSize();
            this.setTitleSprite(this.width, this._titleSpriteHight);
            return;
        }// end function

        protected function updateBtnSize() : void
        {
            if (this._closeBtn)
            {
                this._closeBtn.x = this.width - this._closeBtn.width - 5;
                this._closeBtn.y = 5;
            }
            return;
        }// end function

        public function set layer(param1:ILayer) : void
        {
            this._layer = param1;
            return;
        }// end function

        public function get layer() : ILayer
        {
            return this._layer;
        }// end function

        public function get isHide() : Boolean
        {
            return this._isHide;
        }// end function

        public function hide() : void
        {
            var _loc_1:DisplayObject = null;
            if (this._layer && !this._isHide)
            {
                this._layer.setTop(this);
                Global.stage.focus = Global.stage;
                this.removeDrag();
                Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
                this._isHide = true;
                this._layer.removePopup(this, this._tweenable);
                this.dispatchHideEvent();
                while (this._popupSprite.numChildren > 0)
                {
                    
                    _loc_1 = this._popupSprite.getChildAt(0);
                    if (_loc_1 is IView)
                    {
                        (_loc_1 as IView).hide();
                        continue;
                    }
                    this.removeChild(_loc_1);
                }
            }
            return;
        }// end function

        protected function dispatchHideEvent() : void
        {
            this.dispatchEvent(new WindowEvent(WindowEvent.CLOSE));
            Dispatcher.dispatchEvent(new DataEvent(EventName.WindowClosed, this));
            return;
        }// end function

        public function show(param1:int = 0, param2:int = 0) : void
        {
            this.resetSize();
            if (this._layer)
            {
                if (this._isHide)
                {
                    this._isHide = false;
                    this._layer.addPopUp(this);
                    if (this._isFirstCenter)
                    {
                        this._layer.centerPopup(this);
                        this._isFirstCenter = this._isCenter;
                    }
                    SoundManager.instance.soundPlay(SoundTypeConst.UIclick);
                    this.dispatchShowedEvent();
                }
                else
                {
                    if (!this.parent || this._layer != this.parent)
                    {
                        this._layer.addPopUp(this);
                    }
                    this._layer.setTop(this);
                }
                if (param1 != 0 || param2 != 0)
                {
                    this._layer.setPosition(this, param1, param2);
                }
            }
            return;
        }// end function

        protected function dispatchShowedEvent() : void
        {
            this.dispatchEvent(new WindowEvent(WindowEvent.SHOW));
            Dispatcher.dispatchEvent(new DataEvent(EventName.WindowShowed, this));
            return;
        }// end function

        public function popUp() : void
        {
            if (this.isHide)
            {
                this.show();
            }
            else
            {
                this.hide();
            }
            return;
        }// end function

        public function leftToChat() : void
        {
            if (this.layer is WindowLayer)
            {
                (this.layer as WindowLayer).leftToChat(this);
            }
            return;
        }// end function

        public function rightToTask() : void
        {
            if (this.layer is WindowLayer)
            {
                (this.layer as WindowLayer).rightToTask(this);
            }
            return;
        }// end function

        public function get isDragAble() : Boolean
        {
            return false;
        }// end function

        public function get isDropAble() : Boolean
        {
            return true;
        }// end function

        public function get isThrowAble() : Boolean
        {
            return false;
        }// end function

        public function get dragSource() : Object
        {
            return {};
        }// end function

        public function set dragSource(param1:Object) : void
        {
            return;
        }// end function

        public function canDrop(param1:IDragDrop, param2:IDragDrop) : Boolean
        {
            return true;
        }// end function

        public function addPopUp(param1:DisplayObject, param2:Boolean = false) : void
        {
            if (param1 && this._popupSprite.contains(param1) == false)
            {
                this._popupSprite.addChild(param1);
            }
            return;
        }// end function

        public function centerPopup(param1:DisplayObject) : void
        {
            if (this is WindowLayer)
            {
                param1.x = (Global.stage.stageWidth - param1.width) / 2;
                param1.y = (Global.stage.stageHeight - param1.height) / 2;
            }
            else
            {
                param1.x = (this.width - param1.width) / 2;
                param1.y = (this.height - param1.height) / 2;
            }
            return;
        }// end function

        public function setPosition(param1:DisplayObject, param2:int, param3:int) : void
        {
            param1.x = param2;
            param1.y = param3;
            return;
        }// end function

        public function isTop(param1:DisplayObject) : Boolean
        {
            if (this._popupSprite.contains(param1))
            {
                return this._popupSprite.getChildIndex(param1) == (this._popupSprite.numChildren - 1);
            }
            return false;
        }// end function

        public function removePopup(param1:DisplayObject, param2:Boolean = true) : void
        {
            if (this._popupSprite.contains(param1))
            {
                this._popupSprite.removeChild(param1);
            }
            return;
        }// end function

        public function isPopup(param1:DisplayObject) : Boolean
        {
            return this._popupSprite.contains(param1);
        }// end function

        public function setTop(param1:DisplayObject) : void
        {
            if (this._popupSprite.contains(param1))
            {
                if (this._popupSprite.getChildIndex(param1) != (this._popupSprite.numChildren - 1))
                {
                    this._popupSprite.setChildIndex(param1, (this._popupSprite.numChildren - 1));
                }
            }
            return;
        }// end function

        public function resize(param1:Number, param2:Number) : void
        {
            var _loc_3:DisplayObject = null;
            var _loc_4:int = 0;
            while (_loc_4 < this.numChildren)
            {
                
                _loc_3 = this.getChildAt(_loc_4);
                if (_loc_3 is IView)
                {
                    _loc_3.x = _loc_3.x * param1;
                    _loc_3.y = _loc_3.y * param2;
                }
                _loc_4++;
            }
            return;
        }// end function

        override protected function updateView() : void
        {
            this.updateSize();
            this.updateDisplayList();
            return;
        }// end function

        protected function updateDisplayList() : void
        {
            return;
        }// end function

        public function stageResize() : void
        {
            return;
        }// end function

        public function hideToTarget(param1:DisplayObject = null) : void
        {
            var _loc_2:Point = null;
            if (param1 && param1.parent)
            {
                this._x = this.x;
                this._y = this.y;
                _loc_2 = param1.localToGlobal(new Point(0, 0));
                this._tweenHide = TweenMax.to(this, 0.5, {x:_loc_2.x, y:_loc_2.y, scaleX:0.05, scaleY:0.05, alpha:0, onComplete:this.showTargetFilter, onCompleteParams:[param1]});
            }
            return;
        }// end function

        protected function showTargetFilter(param1:DisplayObject) : void
        {
            EffectManager.glowFilterReg(param1, null, 1, 10, 0, 1);
            return;
        }// end function

        private function resetSize() : void
        {
            var _loc_1:int = 1;
            this.scaleY = 1;
            this.scaleX = _loc_1;
            if (this._x != -1 && y != -1)
            {
                this.x = this._x;
                this.y = this._y;
            }
            if (this._tweenHide && this._tweenHide.active)
            {
                this._tweenHide.kill();
            }
            return;
        }// end function

    }
}
